import {TablePaginationConfig} from "antd/es/table";
import {message} from "antd";

export function nth() {}

export function randomString(e?: number) {
    e = e || 32;
    let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
        a = t.length,
        n = "";
    for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
}

export function isNumeric(value: any) {
    return !isNaN(parseFloat(value)) && !isNaN(value);
}

/**
 * Description: 格式化日期.
 *
 * @param timestamp (number) 时间戳
 * @return (string) "${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}" 的格式化后的日期字符串
 * @author <a href="mailto:scorpio.caplike@gmail.com">LiKe</a> @ 2025-05-04 14:46:29
 */
export function fmtDate(timestamp: number | undefined): string {
    if (!timestamp) {
        return ''
    }

    const date = new Date(timestamp);
    const yyyy = date.getFullYear()
    const mm = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始
    const dd = String(date.getDate()).padStart(2, '0')
    const hh = String(date.getHours()).padStart(2, '0')
    const min = String(date.getMinutes()).padStart(2, '0')
    const ss = String(date.getSeconds()).padStart(2, '0')

    return `${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}`
}

export function fmtMoney(amount: number | undefined) {
    if (!amount) {
        return '¥0.00'
    }
    let [integer, decimal] = amount.toFixed(2).split('.');
    integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    return `¥${integer}.${decimal}`;
}

/**
 * Description: 将毫秒值转换为 HH:mm:ss 的形式.
 *
 * @author <a href="mailto:scorpio.caplike@gmail.com">LiKe</a> @ 2025-05-18 18:56:25
 */
export function formatMilliseconds(ms: number): string {
    let totalSeconds = Math.floor(ms / 1000);
    let hours = Math.floor(totalSeconds / 3600);
    let minutes = Math.floor((totalSeconds % 3600) / 60);
    let seconds = totalSeconds % 60;

    // 补零
    const pad = (num: number) => num.toString().padStart(2, '0');

    return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
}

export interface SimplePagination {
    current: number
    total: number
    pageSize: number
}

export const DEFAULT_PAGINATION: SimplePagination = {
    current: 1,
    total: 0,
    pageSize: 10
}

export interface IPage {
    /** 总数 - total */ total: number
    /** 每页数量 - pageSize */ size: number
    /** 当前页 - current */ current: number
    /** 总页数 - ? */ pages: number
}

export class PageUtil {

    static toDto = function (pagination: SimplePagination | TablePaginationConfig | undefined): IPage | {} {
        if (!pagination) {
            return {}
        }

        let pages = 0
        if (pagination.total && pagination.pageSize) {
            pages = Math.ceil(pagination.total / pagination.pageSize)
        }

        return {
            current: pagination.current, page: pagination.current,
            pages: pages,
            size: pagination.pageSize, limit: pagination.pageSize,
            total: pagination.total
        }
    }

    static toPagination(dto: IPage): SimplePagination {
        return {
            current: Number(dto.current),
            total: Number(dto.total),
            pageSize: Number(dto.size)
        }
    }
}


export class Handler {
    static ofError = function (error: Error | any) {
        message.error(error.message).then(() => {
        })
    }

    static ofHttpWarn = function (httpCode: string | number) {
        message.warn(`Unexpected http status: ${httpCode}`).then(() => {
        })
    }
}
